package com.after.saas.mq.produce;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

/**
 * @Classname RocketMQProducerImpl
 * @Description TODO
 * @auth after
 * @since 2021/8/10 17:02
 */

@Slf4j
@Service
public class RocketMQProducerImpl implements RocketMQProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void sendMessage(String destination, Object message) {
        log.warn("rocketMQProducer {}", rocketMQTemplate);
        try {
            rocketMQTemplate.convertAndSend(destination, message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void sendMessage(String destination, Object message, SendCallback callback) {
        rocketMQTemplate.asyncSend(destination, message, callback);
    }

    @Override
    public void sendMessageInTransaction(String destination, Object msg) {
        rocketMQTemplate.sendMessageInTransaction(destination, MessageBuilder.withPayload(msg).build(), null);
    }

    @Override
    public void sendOneWayMessage(String destination, Object message) {
        rocketMQTemplate.sendOneWay(destination, message);
    }

    @Override
    public SendResult sendOrderMessage(String destination, Object message, String shardingKey) {
        return rocketMQTemplate.syncSendOrderly(destination, message, shardingKey);
    }

    @Override
    public SendResult sendDelayMessage(String destination, Message message, Long delayTimeMillis) {
        return rocketMQTemplate.syncSend(destination, message, delayTimeMillis, -1);
    }

}
